'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { deviceId, status } = event;

  if (!deviceId) {
    return {
      code: -1,
      message: '设备ID不能为空'
    };
  }

  try {
    const result = await db.collection('devices')
      .doc(deviceId)
      .update({
        de_status: status,
        update_time: new Date()
      });

    if (result.updated === 1) {
      return {
        code: 0,
        message: '更新成功'
      };
    } else {
      return {
        code: -1,
        message: '更新失败'
      };
    }
  } catch (e) {
    return {
      code: -1,
      message: '更新设备状态失败',
      error: e.message
    };
  }
}; 